package com.penjin.android.message.company;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.penjin.android.R;
import com.penjin.android.domain.PenjinDepartment;
import com.penjin.android.domain.PenjinEaseUser;
import com.penjin.android.message.utils.EaseUserUtils;

import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;

/**
 * Created by Administrator on 2016/3/14.
 */
public class CompanyStaffAdapter extends BaseExpandableListAdapter {

    Context mContext;
    List<PenjinDepartment> departmentList;
    List<List<PenjinEaseUser>> staffLists;

    public CompanyStaffAdapter(Context context, List<PenjinDepartment> departmentList, List<List<PenjinEaseUser>> lists) {
        this.departmentList = departmentList;
        this.staffLists = lists;
        this.mContext = context;
    }


    public void setData(List<PenjinDepartment> list, List<List<PenjinEaseUser>> lists) {
        this.departmentList = list;
        this.staffLists = lists;
    }

    @Override
    public int getGroupCount() {
        if (this.departmentList == null) {
            return 0;
        }
        return departmentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<PenjinEaseUser> list = staffLists.get(groupPosition);
        return list.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return departmentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<PenjinEaseUser> list = this.staffLists.get(groupPosition);
        return list.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View rootView = LayoutInflater.from(this.mContext).inflate(R.layout.item_company_list_department, parent, false);
        TextView departmentName = (TextView) rootView.findViewById(R.id.departmentName);
        PenjinDepartment department = (PenjinDepartment) this.getGroup(groupPosition);
        departmentName.setText(department.getDepartmentName());
        TextView departmentStaffNum = (TextView) rootView.findViewById(R.id.departmentStaffCount);
        departmentStaffNum.setText(department.getDepartmentStaffNum() + "");
        return rootView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.ease_row_contact, null);
            holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
            holder.nameView = (TextView) convertView.findViewById(R.id.name);
            holder.headerView = (TextView) convertView.findViewById(R.id.header);
            holder.signature = (TextView) convertView.findViewById(R.id.signature);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PenjinEaseUser user = (PenjinEaseUser) getChild(groupPosition, childPosition);
        String realName = user.getRealname();
        //设置昵称
        EaseUserUtils.setUserNick(realName, holder.nameView);
        //设置头像
        EaseUserUtils.setPenjinUserAvatar(mContext, user, holder.avatar);
        EaseUserUtils.setSignature(mContext, user, holder.signature);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolder {
        ImageView avatar;
        TextView nameView;
        TextView headerView;
        TextView signature;
    }

}
